package com.supms.course.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.course.mapper.AttendMapper;
import com.supms.course.domain.Attend;
import com.supms.course.service.IAttendService;

/**
 * 课堂听课Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-02-23
 */
@Service
public class AttendServiceImpl implements IAttendService 
{
    @Autowired
    private AttendMapper attendMapper;

    /**
     * 查询课堂听课
     * 
     * @param id 课堂听课主键
     * @return 课堂听课
     */
    @Override
    public Attend selectAttendById(Long id)
    {
        return attendMapper.selectAttendById(id);
    }

    /**
     * 查询课堂听课列表
     * 
     * @param attend 课堂听课
     * @return 课堂听课
     */
    @Override
    public List<Attend> selectAttendList(Attend attend)
    {
        String operName = SecurityUtils.getUsername();
        attend.setCreateBy(operName);
        return attendMapper.selectAttendList(attend);
    }

    /**
     * 新增课堂听课
     * 
     * @param attend 课堂听课
     * @return 结果
     */
    @Override
    public int insertAttend(Attend attend)
    {
        String operName = SecurityUtils.getUsername();
        attend.setCreateBy(operName);
        String supName = attendMapper.getSupNameByOperName(operName);
        attend.setSupName(supName);
        attend.setCreateTime(DateUtils.getNowDate());
        return attendMapper.insertAttend(attend);
    }

    /**
     * 修改课堂听课
     * 
     * @param attend 课堂听课
     * @return 结果
     */
    @Override
    public int updateAttend(Attend attend)
    {
        String operName = SecurityUtils.getUsername();
        attend.setUpdateBy(operName);
        attend.setUpdateTime(DateUtils.getNowDate());
        return attendMapper.updateAttend(attend);
    }

    /**
     * 批量删除课堂听课，并且批量删除听课评价表
     * 
     * @param ids 需要删除的课堂听课主键
     * @return 结果
     */
    @Override
    public int deleteAttendByIds(Long[] ids)
    {
        // 判断听课表是否做了听课评价，有则先删除听课评价表
        Long[] haveIds = attendMapper.selectAttendEvalFlagByIds(ids);
        if (haveIds.length > 0) {
            attendMapper.deleteAttendEvalByIds(haveIds);
        }

        return attendMapper.deleteAttendByIds(ids);
    }

    /**
     * 删除课堂听课信息
     * 
     * @param id 课堂听课主键
     * @return 结果
     */
    @Override
    public int deleteAttendById(Long id)
    {
        return attendMapper.deleteAttendById(id);
    }
}
